<?php

require_once 'Zend/Validate/Abstract.php';

/**
 * @category   Zend
 * @package    Zend_Validate
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Admin_Validate_FolderExists extends Zend_Validate_Abstract {

   const INVALID      = 'invalid';
   const NOT_EXISTS    = 'notExists';
   const STRING_EMPTY = 'stringEmpty';

   public $_path;
   public $form;
   
   protected static $_filter = null;

   protected $_messageTemplates = array(
      self::INVALID      => "Invalid type given, value should be a string",
      self::NOT_EXISTS    => "folder '%value%' doesn't exist",
      self::STRING_EMPTY => "'%value%' is an empty string"
   );

   
   public function __construct($form, $path='/media/hti') {
      $this->_path = $path;
      $this->form = $form;
   }

   public function isValid($value) {
      
      if (!is_string($value)) {
         $this->_error(self::INVALID);
         return false;
      }

      $this->_setValue($value);

      if ('' === $value) {
         $this->_error(self::STRING_EMPTY);
         return false;
      }

      $absfolder = sprintf('%s/%s/%s/%s', dirname(dirname(dirname(dirname(__FILE__)))), 'public', $this->_path, $value);      
      if(!file_exists($absfolder)) {
         $this->_error(self::NOT_EXISTS);
         return false;
      }
      Zend_Registry::set('absfolder', $absfolder);
      return true;
   }

}
